#include "JSTLModel.h"
#include "JGLWidget.h"
#include "JLog.h"

JSTLModel::JSTLModel(JGLWidget* glwidget, const QString& filename)
:m_Model(filename.toStdString()),
m_bShow(true)
{
	m_ModelDisplayList = glGenLists(1);
	glNewList(m_ModelDisplayList,GL_COMPILE);
	glPushMatrix();
	glBegin(GL_TRIANGLES);
	for (unsigned int i = 0; i < m_Model.numFaces(); i++)
	{
		glNormal3f(m_Model._faces[i]->_normal[0], m_Model._faces[i]->_normal[1], m_Model._faces[i]->_normal[2]);
		glVertex3f(m_Model._faces[i]->_v1[0], m_Model._faces[i]->_v1[1], m_Model._faces[i]->_v1[2]);
		glVertex3f(m_Model._faces[i]->_v2[0], m_Model._faces[i]->_v2[1], m_Model._faces[i]->_v2[2]);
		glVertex3f(m_Model._faces[i]->_v3[0], m_Model._faces[i]->_v3[1], m_Model._faces[i]->_v3[2]);
	}
	glEnd();
	glPopMatrix();
	glEndList();
	Log.Post("STL Model Loaded: " + filename, JLOG_SUCCESS);

	connect(glwidget, SIGNAL(drawNeeded()), this, SLOT(draw()));
	glwidget->updateGL();
}

JSTLModel::~JSTLModel()
{

}

void JSTLModel::draw()
{
	if(m_bShow)
		glCallList(m_ModelDisplayList);
}